import datetime
import socket
import string
import time
import threading
import multiprocessing

HOST, PORT = "localhost", 9999


def request(data, sleep):
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
        sock.connect((HOST, PORT))
        print('Connection success!')
        time.sleep(sleep)
        sock.sendall(bytes(data + "\n", "utf-8"))
        received = str(sock.recv(1024), "utf-8")
        print("{}:Sent:     {}".format(datetime.datetime.now(), data))
        print("{}:Received: {}".format(datetime.datetime.now(), received))


if __name__ == '__main__':
    a = threading.Thread(target=request, args=('A',))
    b = threading.Thread(target=request, args=('B',))
    c = threading.Thread(target=request, args=('C',))
    d = threading.Thread(target=request, args=('D',))

    # a.start()
    # b.start()
    # c.start()
    # d.start()
    time.sleep(2)
    a1 = multiprocessing.Process(target=request, args=('A', 10))
    a2 = multiprocessing.Process(target=request, args=('B', 5))
    a1.start()
    a2.start()
